package com.sl.au.repository;

import java.util.List;

import com.sl.au.entity.Contract;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.MaintainItem;

/**
 * 
 * @author XH
 *
 */
@Transactional(readOnly = true)
public interface MaintainItemRepository extends JpaRepository<MaintainItem, String> {
    /**
     * 售后设备信息处合同数据
     * @param pageable
     * @return
     */
    @Query(" select c from Contract c where auditState=?1 and (isAfterSale = false or isAfterSale is null) order by c.createTime desc ")
    Page<Contract> getAfterSaleContracts1(String auditState,Pageable pageable);
    /**
     * 根据查询条件查询售后设备信息处合同数据
     * @param pageable
     * @return
     */
    @Query(" select c from Contract c where auditState=?2 and (isAfterSale = false or isAfterSale is null) and ( c.supplierCode like '%'+?1+'%' or "
            + "c.projectName like '%'+?1+'%' or c.orderUnit like '%'+?1+'%' or c.orderAddress like '%'+?1+'%' )"
            + " order by c.createTime desc ")
    Page<Contract> getAfterSaleContracts2(String key,String auditState,Pageable pageable);
    /**
     * 根据查询条件查询售后设备信息处审批已通过的合同数据
     * @param pageable
     * @return
     */
    @Query(" select c from Contract c where c.auditState = '通过' and ( c.isAfterSale = false or c.isAfterSale is null ) "
    		+ " and ( c.supplierCode like '%'+?1+'%' or c.projectName like '%'+?1+'%' or c.orderUnit like '%'+?1+'%' "
    		+ "or c.orderAddress like '%'+?1+'%' ) order by c.createTime desc ")
    Page<Contract> getPassContracts(String key,Pageable pageable);
    /**
     * 根据查询条件查询售后设备信息处合同数据条数
     * @param key
     * @return
     */
    @Query(" select count(c) from Contract c where (isAfterSale = false or isAfterSale is null) and auditState=?2 and ( c.supplierCode like '%'+?1+'%' or "
            + "c.projectName like '%'+?1+'%' or c.orderUnit like '%'+?1+'%' or c.orderAddress like '%'+?1+'%' )")
    long getAfterSaleContractsCount2(String key,String auditState);
    /**
     * 根据查询条件查询售后设备信息处审批已通过的合同数据条数
     * @param key
     * @return
     */
    @Query(" select count(c) from Contract c where c.auditState = '通过' and ( c.isAfterSale = false or c.isAfterSale is null ) "
    		+ " and ( c.supplierCode like '%'+?1+'%' or c.projectName like '%'+?1+'%' or c.orderUnit like '%'+?1+'%' "
    		+ "or c.orderAddress like '%'+?1+'%' ) ")
    long getPassContractsCount(String key);
    /**
     * 售后设备信息处合同数据条数
     * @return
     */
    @Query(" select count(c) from Contract c where auditState=?1 and (isAfterSale = false or isAfterSale is null)")
    long getAfterSaleContractsCount1(String auditState);
}
